package com.ychs.dpmms.controller;

import com.ychs.dpmms.entity.ResultMessage;
import com.ychs.dpmms.entity.SecondOrganization;
import com.ychs.dpmms.service.IBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:二级基层党组织Controller层
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/8/31
 */
//解决跨域问题
@CrossOrigin
//相当于方法@RequestBody+类@Controller
@RestController
@RequestMapping("/secondOrganization")
public class SecondOrganizationController {
    //日志对象
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    //业务对象
    @Resource(name = "secondOrganizationService")
    private IBaseService<SecondOrganization> service;

    //添加方法
    @PostMapping("/add")
    public ResultMessage add(@Validated SecondOrganization secondOrganization, BindingResult br , Map map) {
        logger.debug("enter secondOrganization add");
        String msg = "增加成功";
        boolean result = false;
        /*校验不通过，会有错误信息*/
        if (br.hasFieldErrors()) {
            for (FieldError fe : br.getFieldErrors()){
                if (!fe.getField().equals("secondOrganization")) {
                    logger.debug("校验失败"+fe);
                    return new ResultMessage(false, null, "校验失败");
                }
            }
        }else{
            logger.debug("secondOrganization="+secondOrganization);

            //判断该二级党组织名称是否已经存在
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("name", secondOrganization.getName());
            int count = service.searchTotalNumber(conditions);
            //若查到该名称的数量大于等于1，则不添加，并返回信息，否则继续执行添加
            if(count>=1){
                msg = "该名称已经存在！";
            }else{
                result = service.add(secondOrganization);
            }
        }
        logger.debug("secondOrganization add result = "+ result);
        ResultMessage resultMessage = new ResultMessage(result, secondOrganization, msg);
        return resultMessage;
    }

    //修改方法
    @PostMapping("/modify")
    public ResultMessage modify(@Validated SecondOrganization secondOrganization, BindingResult br , Map map) {
        logger.debug("enter secondOrganization modify");
        String msg = "修改成功";
        boolean result = false;
        /*校验不通过，会有错误信息*/
        if (br.hasFieldErrors()) {
            for (FieldError fe : br.getFieldErrors()){
                if (!fe.getField().equals("secondOrganization")) {
                    logger.debug("校验失败"+fe);
                    return new ResultMessage(false, null, "校验失败");
                }
            }
        }else{
            logger.debug("secondOrganization="+secondOrganization);

            //与添加种的重复判断不同
            //接受用户前台输入修改后的信息，判断是否存在，不是该id，但是是该名称的信息数量
            //即达到修改为原来的信息可行，但是若修改后与别的信息重复，不可行
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("name", secondOrganization.getName());
            conditions.put("notId", secondOrganization.getId());
            int count = service.searchTotalNumber(conditions);
            //若查到该名称的数量大于等于1，则不添加，并返回信息，否则继续执行添加
            if(count>=1){
                msg = "该名称已经存在！";
            }else{
                result = service.modify(secondOrganization);
            }
        }
        logger.debug("secondOrganization add result = "+ result);
        ResultMessage resultMessage = new ResultMessage(result, secondOrganization, msg);
        return resultMessage;
    }

    //查询方法
    @GetMapping("/search")
    public ResultMessage search(String name,Integer limit,Integer page) {
        logger.debug("name="+name+" limit="+limit+" page="+page);
        Map<String, Object> conditions = new HashMap<>();
        if (name != null && !"".equals(name)) {
            conditions.put("name", name);
        }
        int count = service.searchTotalNumber(conditions);
        if (limit != null && page != null) {
            conditions.put("begin",(page-1)*limit);
            conditions.put("pageSize", limit);
        }
        List<SecondOrganization> secondOrganizationList = service.search(conditions);
        logger.debug(secondOrganizationList.toString());
        ResultMessage resultMessage
                = new ResultMessage(true, secondOrganizationList, ""+count);
        return resultMessage;
    }

    @PostMapping("/remove/{id}")
    public ResultMessage remove(@PathVariable("id") int id){
        logger.debug("enter secondOrganization remove");
        logger.debug("id="+id);
        boolean result = service.remove(id);
        logger.debug("result = " + result);
        ResultMessage resultMessage =
                new ResultMessage(result, null, result?"删除成功":"删除失败");
        return resultMessage;
    }
}
